package com.neudu.glsb.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.neudu.glsb.commons.JSONReturn;
import com.neudu.glsb.pojo.Products;
import com.neudu.glsb.pojo.ShoppingCart;
import com.neudu.glsb.service.ProductsService;
import com.neudu.glsb.service.ShoppingCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class ShoppingCartController {

    @Autowired
    private ShoppingCartService shoppingCartService;

    @Autowired
    private ProductsService productService;

    @Autowired
    private JSONReturn jsonReturn;

    /**
     * 添加商品到购物车
     * @param shoppingCart 购物车对象，包含用户ID、产品ID和数量
     * @return 操作结果
     */
//    @PostMapping("/addToCart")
//    public String addToCart(@RequestBody ShoppingCart shoppingCart) {
//        try {
//            if (ObjectUtils.isEmpty(shoppingCart.getUserId()) ||
//                    ObjectUtils.isEmpty(shoppingCart.getProductId()) ||
//                    shoppingCart.getQuantity() <= 0) {
//                return jsonReturn.returnError("购物车项信息不完整");
//            }
//            boolean success = shoppingCartService.saveOrUpdate(shoppingCart);
//            if (success) {
//                return jsonReturn.returnSuccess("添加到购物车成功");
//            } else {
//                return jsonReturn.returnError("添加到购物车失败");
//            }
//        } catch (Exception e) {
//            e.printStackTrace();
//            return jsonReturn.returnError(e.getMessage());
//        }
//    }

    /**
     * 获取购物车列表
     * @param userId 用户ID
     * @return 购物车列表
     */
    @GetMapping("/getCartItems")
    public String getCartItems(@RequestParam Integer userId) {
        try {
            if (ObjectUtils.isEmpty(userId)) {
                return jsonReturn.returnError("用户ID不能为空");
            }
            LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(ShoppingCart::getUserId, userId);
            List<ShoppingCart> cartItems = shoppingCartService.list(queryWrapper);
            for (ShoppingCart cartItem : cartItems) {
                Products product = productService.getById(cartItem.getProductId());
                cartItem.setProduct(product); // 将商品信息关联到购物车项
            }
            return jsonReturn.returnSuccess(cartItems);
        } catch (Exception e) {
            e.printStackTrace();
            return jsonReturn.returnError(e.getMessage());
        }
    }

    /**
     * 更新购物车商品数量
     * @param shoppingCart 购物车对象，包含购物车ID和更新后的数量
     * @return 操作结果
     */
    @PostMapping("/updateCartItemQuantity")
    public String updateCartItemQuantity(@RequestBody ShoppingCart shoppingCart) {
        try {
            if (ObjectUtils.isEmpty(shoppingCart.getId()) || shoppingCart.getQuantity() <= 0) {
                return jsonReturn.returnError("购物车项ID或数量不合法");
            }
            LambdaUpdateWrapper<ShoppingCart> updateWrapper = new LambdaUpdateWrapper<>();
            updateWrapper.eq(ShoppingCart::getId, shoppingCart.getId())
                    .set(ShoppingCart::getQuantity, shoppingCart.getQuantity());
            boolean success = shoppingCartService.update(updateWrapper);
            if (success) {
                return jsonReturn.returnSuccess("更新购物车数量成功");
            } else {
                return jsonReturn.returnError("更新购物车数量失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return jsonReturn.returnError(e.getMessage());
        }
    }

    /**
     * 删除购物车商品
     * @param id 购物车项ID
     * @return 操作结果
     */
    @PostMapping("/removeCartItem")
    public String removeCartItem(@RequestParam Integer id) {
        try {
            if (ObjectUtils.isEmpty(id)) {
                return jsonReturn.returnError("购物车项ID不能为空");
            }
            boolean success = shoppingCartService.removeById(id);
            if (success) {
                return jsonReturn.returnSuccess("移除购物车商品成功");
            } else {
                return jsonReturn.returnError("移除购物车商品失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return jsonReturn.returnError(e.getMessage());
        }
    }

    /**
     * 清空购物车
     * @param userId 用户ID
     * @return 操作结果
     */
    @PostMapping("/clearCart")
    public String clearCart(@RequestParam Integer userId) {
        try {
            if (ObjectUtils.isEmpty(userId)) {
                return jsonReturn.returnError("用户ID不能为空");
            }
            LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(ShoppingCart::getUserId, userId);
            boolean success = shoppingCartService.remove(queryWrapper);
            if (success) {
                return jsonReturn.returnSuccess("清空购物车成功");
            } else {
                return jsonReturn.returnError("清空购物车失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return jsonReturn.returnError(e.getMessage());
        }
    }
}